<?php

namespace App\Models;

use Illuminate\Support\Str;

class Attachment extends Base
{
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'wx_media_response' => 'json',
    ];

    protected static function boot()
    {
        static::bootTraits();
    }

    /**
     *  URL 访问器, 加上 OSS 域名(CDN)
     */
    public function getUrlAttribute($value)
    {
        $isImg = Str::contains($this['mime_type'] ?? '', 'image')
            || Str::contains($value, 'jpeg')
            || Str::contains($value, 'png')
            || Str::contains($value, 'webp')
            || Str::contains($value, 'x-ms-bmp')
            || Str::contains($value, 'heic');

        return env('OSS_CDN_BASE_URI') . $this->file . ($isImg ? '?x-oss-process=image/resize,p_50' : ''); // 缩略图
    }
}
